package com.hj.saapp.control.activity;

import android.app.DatePickerDialog;
import android.text.TextUtils;
//import android.view.MotionEvent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hj.saapp.R;
import com.hj.saapp.common.manager.AppManager;
import com.hj.saapp.common.utils.HttpHelper;
import com.hj.saapp.control.activity.base.BaseActivity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import lib.hj.com.commonlib.bean.BaseBean;

public class MyCaseSearchActivity extends BaseActivity implements View.OnClickListener {

    private String jybh = "";
    private String yhm = "";
    private String bmbh = "";
    private String ajbh = "";
    private String ajlx = "";
    private String ajmc = "";
    private String biginTime = "";
    private String endTime = "";


    private EditText JYBHEtMySearch;//警员编号
    private EditText YHMEtMySearch;//用户名
    private EditText BMBHEtMySearch;//部门编号
    private EditText AJBHEtMySearch;//案件编号
    private EditText AJLXEtMySearch;//案件类型
    private EditText AJMCEtMySearch;//案件名称
    private Button BEGINTIMETvMySearch;//开始时间
    private Button ENDTIMEtvMySearch;//结束时间
    private Button sumbitBtnMySearch;//提交
    private TextView textView;

    private int dateType = 0;

    int year;
    int month;
    int day;

    @Override
    public int getLayoutByid() {
        return R.layout.activity_my_case_search;
    }

    @Override
    public int getTitleById() {
        return R.string.my_case_search_tille;
    }

    @Override
    public void initView() {
        JYBHEtMySearch = (EditText) findViewById(R.id.JYBHEt_mysearch);
        YHMEtMySearch = (EditText) findViewById(R.id.YHMEt_mysearch);
        BMBHEtMySearch = (EditText) findViewById(R.id.BMBHEt_mysearch);
        AJBHEtMySearch = (EditText) findViewById(R.id.AJBHEt_mysearch);
        AJLXEtMySearch = (EditText) findViewById(R.id.AJLXEt_mysearch);
        AJMCEtMySearch = (EditText) findViewById(R.id.AJMCEt_mysearch);
        BEGINTIMETvMySearch = (Button) findViewById(R.id.BEGINTIMETv_mysearch);
        ENDTIMEtvMySearch = (Button) findViewById(R.id.ENDTIMETv_mysearch);
        sumbitBtnMySearch = (Button) findViewById(R.id.submit_btn_mysearch);
        textView = (TextView) findViewById(R.id.search_view);
//        initDate();//初始化时间
    }

    //设置默认的时间
    private void initDate() {
        //初始化Calendar日历对象
        Calendar mycalendar = Calendar.getInstance(Locale.CHINA);
        Date mydate = new Date(); //获取当前日期Date对象
        mycalendar.setTime(mydate);////为Calendar对象设置时间为当前日期
        int year = mycalendar.get(Calendar.YEAR); //获取Calendar对象中的年
        int month = mycalendar.get(Calendar.MONTH);//获取Calendar对象中的月
        int day = mycalendar.get(Calendar.DAY_OF_MONTH);//获取这个月的第几天
        month += 1;
        ENDTIMEtvMySearch.setText(year + "-" + month + "-" + day);
        BEGINTIMETvMySearch.setText(year + "-" + month + "-" + day);
    }

    public void showDate() {
        //初始化Calendar日历对象
        Calendar mycalendar = Calendar.getInstance(Locale.CHINA);
        Date mydate = new Date(); //获取当前日期Date对象
        mycalendar.setTime(mydate);////为Calendar对象设置时间为当前日期
        year = mycalendar.get(Calendar.YEAR); //获取Calendar对象中的年
        month = mycalendar.get(Calendar.MONTH);//获取Calendar对象中的月
        day = mycalendar.get(Calendar.DAY_OF_MONTH);//获取这个月的第几天
        DatePickerDialog datePickerDialog = new DatePickerDialog(MyCaseSearchActivity.this, Datelistener, year, month, day);//显示日历
        datePickerDialog.show();
    }

    @Override
    public void initListener() {
        sumbitBtnMySearch.setOnClickListener(this);
        BEGINTIMETvMySearch.setOnClickListener(this);
        ENDTIMEtvMySearch.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.BEGINTIMETv_mysearch:
                dateType = 0;
                showDate();
//                BEGINTIMETvMySearch.setText( year + "-" + (month + 1) + "-" + day);
                break;
            case R.id.ENDTIMETv_mysearch:
                dateType = 1;
                showDate();
//                Toast.makeText(this,"提交失败",Toast.LENGTH_SHORT).show();
                break;
            case R.id.submit_btn_mysearch:
                getInitData();
                prepareLoadData();
                break;
        }
    }

    @Override
    public void getHttpData() throws Exception {

        Map<String, String> map = new HashMap<>();
        String data = "";
        if (TextUtils.isEmpty(ajmc)) {

            data = "{\"AJBH\":\"" + ajbh + "\",\"AJLX\":\"" + ajlx + "\",\"AJMC\":\"\",\"BEGINTIME\":\"" + biginTime + "\",\"ENDTIME\":\"" + endTime + "\"}";

        } else {
            data = "{\"AJBH\":\"" + ajbh + "\",\"AJLX\":\"" + ajlx + "\",\"AJMC\":\"\\u0025" + ajmc + "\\u0025\",\"BEGINTIME\":\"" + biginTime + "\",\"ENDTIME\":\"" + endTime + "\"}";

        }

        String log = "{\"DEPARTMENTCODE\":\"" + bmbh + "\",\"USER_ID\":\"" + jybh + "\",\"USER_NAME\":\"" + yhm + "\"}";

        map.put("log", log);
        map.put("data", data);

        HttpHelper.searchCase(this, map);
//       HttpHelper.getFinancialInfo(this);
    }

    private void getInitData() {
        jybh = JYBHEtMySearch.getText().toString();
        yhm = YHMEtMySearch.getText().toString();
        bmbh = BMBHEtMySearch.getText().toString();
        ajbh = AJBHEtMySearch.getText().toString();
        if ("01".equals(AJLXEtMySearch.getText().toString()) || "1".equals(AJLXEtMySearch.getText().toString())) {
            ajlx = AJLXEtMySearch.getText().toString();
            ajlx = "行政";
        } else if ("02".equals(AJLXEtMySearch.getText().toString()) || "2".equals(AJLXEtMySearch.getText().toString())) {
            ajlx = AJLXEtMySearch.getText().toString();
            ajlx = "刑事";
        } else {
            Toast.makeText(this, "请输入正确的案件类型", Toast.LENGTH_SHORT).show();
            return;
        }
        ajmc = AJMCEtMySearch.getText().toString();
        biginTime = BEGINTIMETvMySearch.getText().toString();
        endTime = ENDTIMEtvMySearch.getText().toString();


    }


    public DatePickerDialog.OnDateSetListener Datelistener = new DatePickerDialog.OnDateSetListener() {

        /**params：view：该事件关联的组件
         * params：myyear：当前选择的年
         * params：monthOfYear：当前选择的月
         * params：dayOfMonth：当前选择的日
         */
        @Override
        public void onDateSet(DatePicker view, int myyear, int monthOfYear, int dayOfMonth) {
            //修改year、month、day的变量值，以便以后单击按钮时，DatePickerDialog上显示上一次修改后的值
            monthOfYear += 1;
            switch (dateType) {
                case 0:
                    Date date1 = null;
                    Date date2 = null;
                    String select = myyear + "-" + monthOfYear + "-" + dayOfMonth + " 00:00:00";
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    try {
                        date1 = sdf.parse(select);
                        date2 = sdf.parse(ENDTIMEtvMySearch.getText().toString() + " 23:59:59");
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    if (date1 != null && date2 != null) {
                        if (date1.getTime() > date2.getTime()) {
                            BaseActivity baseActivity = (BaseActivity) AppManager.getAppManager().getActivity();
                            baseActivity.showPopDialog("开始时间不能大于结束时间");
                        } else {
                            BEGINTIMETvMySearch.setText(myyear + "-" + monthOfYear + "-" + dayOfMonth);
                        }
                    }
                    BEGINTIMETvMySearch.setText(myyear + "-" + monthOfYear + "-" + dayOfMonth);
                    break;
                case 1:
                    ENDTIMEtvMySearch.setText(myyear + "-" + monthOfYear + "-" + dayOfMonth);
                    break;
            }

        }

    };

    @Override
    public void onSuccess(BaseBean paramObject) {
        super.onSuccess(paramObject);
        Log.i("dasepara", paramObject.toString());
        Toast.makeText(this, "提交成功", Toast.LENGTH_SHORT).show();
        textView.setText(paramObject+"");
//        changeTo(MyCaseDetailedInfoActivity.class,paramObject);


    }

    @Override
    public void onFailure(int paramInt, String paramString) {
        Toast.makeText(this, "提交失败", Toast.LENGTH_SHORT).show();
        showPopDialog(paramString);
        String data=paramInt+" "+paramString;
        textView.setText(data);
    }
}
